#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <netdb.h>
#include <arpa/ftp.h>
#include <errno.h>
#include "linux_ftp.h"
#include "linux_socket.h"  //zxr add
#include "../ping.h"
/*
#define _DEBUG
*/
FTPError_t g_FTPErrorNo = -1;//ＦＴＰ错误信息返回码，自定义错误码，用于发送错误信息上报
//typedef unsigned char byte;
bool Ftp_GetReply(SFtp_Ftp* ftp)
{
	char recvbuf[FTP_BUFSIZE];
	
	if (ftp->CommandChunnel==INVALID_SOCKET)
	{
		printf("Does not connect\n");
		return false;
	}
	ftp->nReplyCode = 0;
	memset(ftp->szReplyString, 0, FTP_BUFSIZE);

	while (FPF_SockSelect(ftp->CommandChunnel, FD_READ, ftp->nMsTimeOut))
	{
		memset(recvbuf, 0, FTP_BUFSIZE);
		if (FPF_SockReceive(ftp->CommandChunnel, recvbuf, FTP_BUFSIZE-1)<=0)
		{
			printf("Receive reply failed\n");
			g_FTPErrorNo = Conn_Error;//FTP上传失败: 无法连接FTP服务器!
			return false;
		}
#ifdef _DEBUG
		printf(recvbuf);
#endif
		if (recvbuf[strlen(recvbuf)-1]=='\n')
		{
			char* lastline = recvbuf+strlen(recvbuf)-2;
			for(; *lastline!='\n' && lastline>recvbuf; lastline--);
			if (*lastline=='\n') 
				lastline++;
			if (lastline[3] == ' ')
			{
				ftp->nReplyCode = atoi(lastline);
				strcpy(ftp->szReplyString, lastline+4);
				return true;
			}
		}
	}
	return false;
}

bool Ftp_SendCommand(SFtp_Ftp* ftp, const char* lpszCommand)
{
	char command[256];
	if (ftp->CommandChunnel==INVALID_SOCKET)
	{
		printf("Does not connect\n");
		return false;
	}
#ifdef _DEBUG
	if (strncmp(lpszCommand, "PASS ", 5)!=0)
		printf("CMD>%s\n", lpszCommand);
	else
		printf("CMD>PASS *****\n");
#endif
	sprintf(command, "%s\r\n", lpszCommand);
	if (FPF_SockSend(ftp->CommandChunnel, command, strlen(command))<=0)
	{
		printf("Send command '%s' failed\n", lpszCommand);
		return false;
	}
	return Ftp_GetReply(ftp);
}

SFtp_Ftp* Ftp_Connect(const char* lpszHostAddress, const char* lpszUser, const char* lpszPassword)
{
	u_int nFtpPort;
	struct protoent * lpProtocol;
	char szCommand[256];
	SFtp_Ftp* ftp = (SFtp_Ftp*)malloc(sizeof(SFtp_Ftp));
	if(ftp == NULL)
	{
		printf(" malloc SFtp_Ftp  failed \n");
		return NULL;
	}
	ftp->CommandChunnel = INVALID_SOCKET;
	ftp->DataChunnel = INVALID_SOCKET;
	//ftp->pFtpList = NULL;
	ftp->nFtpListCount = 0;
	//ftp->nTransferMode = FPD_ModePassive;
	ftp->nTransferMode = FPD_ModePort;
	ftp->nTransferType = FPD_TypeBinary;
	ftp->nReplyCode = 0;
	ftp->nMsTimeOut = 60000;
	memset(ftp->szReplyString, 0, FTP_BUFSIZE);
	ftp->CommandChunnel = FPF_SockCreate(0, SOCK_STREAM);

	if (ftp->CommandChunnel == INVALID_SOCKET)
	{
		perror("ftp upload:");
		Ftp_Close(ftp);
		return NULL;
	}
	lpProtocol = getprotobyname("ftp");
	if (lpProtocol==NULL)
		nFtpPort = 21;
	else
		nFtpPort = lpProtocol->p_proto;
	if (!FPF_SockConnect(ftp->CommandChunnel, lpszHostAddress, nFtpPort))
	{
		Ftp_Close(ftp);
		return NULL;
	}
	if(Ftp_GetReply(ftp) == false)
	{
		Ftp_Close(ftp);
		return NULL;
	}
	if (!lpszUser || !*lpszUser)
		strcpy(szCommand, "USER Anonymous");
	else
		sprintf(szCommand, "USER %s", lpszUser);
	if (Ftp_SendCommand(ftp, szCommand) && ftp->nReplyCode/100==CONTINUE)
	{
		if (!lpszUser || !*lpszUser)
			strcpy(szCommand, "PASS user@host");
		else
			sprintf(szCommand, "PASS %s", lpszPassword);

		if (Ftp_SendCommand(ftp, szCommand) && ftp->nReplyCode/100==COMPLETE)
		{
			if (Ftp_SendCommand(ftp, "SYST") && ftp->nReplyCode/100==COMPLETE)
				strcpy(ftp->strSystemType, ftp->szReplyString);

			Ftp_SetTransferType(ftp, ftp->nTransferType);
			return ftp;
		}
		else
		{
			g_FTPErrorNo = PASS_230_;//FTP上传失败: 服务器登陆密码错误!
			Ftp_Close(ftp);
			return NULL;
		}
	}
	g_FTPErrorNo = USER_331_;//FTP上传失败: 无效的用户名!
	Ftp_Close(ftp);
	return NULL;
}

void Ftp_Close(SFtp_Ftp* ftp)
{
	if (ftp!=NULL)
	{
		if (ftp->DataChunnel!=INVALID_SOCKET)
		{
			FPF_SockClose(ftp->DataChunnel);
			ftp->DataChunnel = INVALID_SOCKET;
		}

		if (ftp->CommandChunnel!=INVALID_SOCKET)
		{
			Ftp_SendCommand(ftp, "QUIT");
			FPF_SockClose(ftp->CommandChunnel);
			ftp->CommandChunnel = INVALID_SOCKET;
		}
		free(ftp);
		ftp = NULL;
	}
}

bool Ftp_SetTransferType(SFtp_Ftp* ftp, int nType)
{
	bool bResult;
	if (nType==FPD_TypeAscii)
		bResult = Ftp_SendCommand(ftp, "TYPE A");
	else
		bResult = Ftp_SendCommand(ftp, "TYPE I");
	if (bResult)
		ftp->nTransferType = nType;
	
	return bResult;
}

void Ftp_SetTransferMode(SFtp_Ftp* ftp, int nMode)
{
	ftp->nTransferMode = nMode;
}

void Ftp_SetTimeOut(SFtp_Ftp* ftp, int nMsTimeOut)
{
	ftp->nMsTimeOut = nMsTimeOut;
}

bool Ftp_GetCurrentDirectory(SFtp_Ftp* ftp, char* lpszCurrentDirectory)
{
	char *first = NULL, *last = NULL;
	
	if (!Ftp_SendCommand(ftp, "PWD") || ftp->nReplyCode/100!=COMPLETE)
		return false;
	first = strchr(ftp->szReplyString, '\"');
	last = strrchr(ftp->szReplyString, '\"');
	strncpy(lpszCurrentDirectory, first+1, last-first-1);
	lpszCurrentDirectory[last-first-1] = '\0';
	return true;
}

bool Ftp_SetCurrentDirectory(SFtp_Ftp* ftp, const char* lpszCurrentDirectory)
{
	char sCommand[256];
	sprintf(sCommand, "CWD %s", lpszCurrentDirectory);
	return (Ftp_SendCommand(ftp, sCommand) && ftp->nReplyCode/100==COMPLETE);
}

bool Ftp_CreateDirectory(SFtp_Ftp* ftp, const char* lpszDirectory)
{
	char szCommand[256];

	sprintf(szCommand, "MKD %s", lpszDirectory);
	return (Ftp_SendCommand(ftp, szCommand) && ftp->nReplyCode/100==COMPLETE);
}

bool Ftp_RemoveDirectory(SFtp_Ftp* ftp, const char* lpszDirectory)
{
	char szCommand[256];

	sprintf(szCommand, "RMD %s", lpszDirectory);
	return (Ftp_SendCommand(ftp, szCommand) && ftp->nReplyCode/100==COMPLETE);
}

bool Ftp_RenameFile(SFtp_Ftp* ftp, const char* lpszExisting, const char* lpszNew)
{
	char szCommand[256];

	sprintf(szCommand, "RNFR %s", lpszExisting);
	if (Ftp_SendCommand(ftp, szCommand) && ftp->nReplyCode/100==CONTINUE)
	{
		sprintf(szCommand, "RNTO %s", lpszNew);
		if (Ftp_SendCommand(ftp, szCommand) && ftp->nReplyCode/100==COMPLETE)
			return true;

		g_FTPErrorNo = RNTO_250_;//FTP上传失败: 无法重命名图片到指定文件名
	}
	g_FTPErrorNo = RNFR_350_;//FTP上传失败: 无法重命名, 临时文件丢失!
	return false;
}

bool Ftp_DeleteFile(SFtp_Ftp* ftp, const char* lpszFileName)
{
	char szCommand[256];

	sprintf(szCommand, "DELE %s", lpszFileName);
	return (Ftp_SendCommand(ftp, szCommand) && ftp->nReplyCode/100==COMPLETE);
}

int Ftp_GetFileSize(SFtp_Ftp* ftp, const char* lpszRemoteFile)
{
	char szCommand[256];
	int nFileSize = -1;

	sprintf(szCommand, "SIZE %s", lpszRemoteFile);
	if (!Ftp_SendCommand(ftp, szCommand) || ftp->nReplyCode/100!=COMPLETE)
		return -1;
	nFileSize = atoi(ftp->szReplyString);
	return nFileSize;
}

bool Ftp_GetFileTime(SFtp_Ftp* ftp, const char* lpszRemoteFile, struct tm* ptmFileTime)
{
	char szCommand[256];
	char file_time[16];
	struct tm gm, *local, locTimeSc;
	time_t t;

	sprintf(szCommand, "MDTM %s", lpszRemoteFile);
	if (!Ftp_SendCommand(ftp, szCommand) || ftp->nReplyCode/100!=COMPLETE)
		return false;
	strcpy(file_time, ftp->szReplyString);
	
	sscanf(file_time, "%04d%02d%02d%02d%02d%02d",
		&gm.tm_year, &gm.tm_mon, &gm.tm_mday, &gm.tm_hour, &gm.tm_min, &gm.tm_sec);
	gm.tm_year -= 1900;
	gm.tm_mon -= 1;
	t = mktime(&gm);
	t -= timezone;
	localtime_r(&t, &locTimeSc);
	local = &locTimeSc;
	*ptmFileTime = *local;
	
	return true;
}

bool Ftp_InitDataChunnel(SFtp_Ftp* ftp)
{
	if (ftp->DataChunnel!=INVALID_SOCKET)
		FPF_SockClose(ftp->DataChunnel);
	if (ftp->nTransferMode==FPD_ModePassive)
	{
		u_long a1,a2,a3,a4,p1,p2;
		char pasv[32];
		char host_addr[16];
		
		ftp->DataChunnel = FPF_SockCreate(0, SOCK_STREAM);
		if (ftp->DataChunnel==INVALID_SOCKET)
			return false;
		if (!Ftp_SendCommand(ftp, "PASV") || ftp->nReplyCode/100!=COMPLETE)
			return false;
		
		strcpy(pasv, strchr(ftp->szReplyString, '('));
		sscanf(pasv, "(%ld,%ld,%ld,%ld,%ld,%ld)", &a1, &a2, &a3, &a4, &p1, &p2);
		sprintf(host_addr, "%d.%d.%d.%d", (int)a1, (int)a2, (int)a3, (int)a4);
		if (!FPF_SockConnect(ftp->DataChunnel, host_addr, (p1 << 8) | p2))
			return false;
	}
	else
	{
		char *a, *p;
		char szCommand[256];
		struct sockaddr_in data_addr;
		
		size_t len = sizeof(data_addr);
		if (-1==getsockname(ftp->CommandChunnel, (struct sockaddr*)&data_addr, &len))
			return false;
		data_addr.sin_port = 0;
		
		ftp->DataChunnel = FPF_Socket(SOCK_STREAM);
		if (ftp->DataChunnel ==INVALID_SOCKET)
			return false;
		if (-1==bind(ftp->DataChunnel, (struct sockaddr*)&data_addr, sizeof(data_addr)))
			return false;
		if (-1==getsockname(ftp->DataChunnel, (struct sockaddr*)&data_addr, &len))
			return false;
		if (!FPF_SockListen(ftp->DataChunnel, 1))
			return false;
		a = (char *)&data_addr.sin_addr;
		p = (char *)&data_addr.sin_port;
#define	UC(b)	(((int)b)&0xff)
		sprintf(szCommand, "PORT %d,%d,%d,%d,%d,%d",
			UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
		if (!Ftp_SendCommand(ftp, szCommand) || ftp->nReplyCode/100==ERROR)
		{
			g_FTPErrorNo = PORT_220_;//FTP上传失败: 请求数据连接失败
			return false;
		}
	}
	
	return true;
}

bool Ftp_BuildDataChunnel(SFtp_Ftp* ftp)
{
	ftp_scoket s = INVALID_SOCKET;
	
	if (ftp->nTransferMode==FPD_ModePassive)
		return true;
	if (!FPF_SockSelect(ftp->DataChunnel, FD_ACCEPT, ftp->nMsTimeOut))
		return false;
	s = FPF_SockAccept(ftp->DataChunnel);
	if (s==INVALID_SOCKET)
		return false;
	FPF_SockClose(ftp->DataChunnel);
	ftp->DataChunnel = s;
	
	return true;
}

bool Ftp_GetFile(SFtp_Ftp* ftp, const char* lpszRemoteFile, const char* lpszNewFile)
{
	#define DEBUG false
	char szCommand[256];
	FILE *fp_recv = fopen(lpszNewFile, (ftp->nTransferType==FPD_TypeAscii) ? "wt" : "wb" );
	if (fp_recv==NULL)
	{
		printf("Can not write file %s\n", lpszNewFile);
		if (DEBUG) printf("1");
		return false;
	}
	if (!Ftp_InitDataChunnel(ftp))
	{
		FPF_SockClose(ftp->DataChunnel);
		fclose(fp_recv);
		if (DEBUG) printf("2");
		return false;
	}
	sprintf(szCommand, "RETR %s", lpszRemoteFile);
	if (DEBUG) printf("szCommand:%s\n",szCommand);
	if (!Ftp_SendCommand(ftp, szCommand) || ftp->nReplyCode/100!=PRELIM)
	{
		FPF_SockClose(ftp->DataChunnel);
		fclose(fp_recv);
		if (DEBUG) printf("3");
		return false;
	}
	if (!Ftp_BuildDataChunnel(ftp))
	{
		FPF_SockClose(ftp->DataChunnel);
		Ftp_GetReply(ftp);
		fclose(fp_recv);
		if (DEBUG) printf("4");
		return false;
	}

	while(true)
	{
		char recvbuf[FTP_BUFSIZE+1];
		int len = 0;
		memset(recvbuf, 0, FTP_BUFSIZE+1);
		len = FPF_SockReceive(ftp->DataChunnel, recvbuf, FTP_BUFSIZE);
		if (len<=0 || len>FTP_BUFSIZE)
		{
			FPF_SockClose(ftp->DataChunnel);
			break;
		}
		if (ftp->nTransferType==FPD_TypeAscii)
		{
			char tempbuf[FTP_BUFSIZE+1];
			char *p1, *p2;
			memset(tempbuf, 0, FTP_BUFSIZE+1);
			p1 = recvbuf;
			p2 = tempbuf;
			for( ; *p1; p1++)
				if (*p1 != '\r')
					*p2++ = *p1;
			len = strlen(tempbuf);
			strcpy(recvbuf, tempbuf);
		}
		fwrite(recvbuf, len, 1, fp_recv);
	}
	fclose(fp_recv);

	
	if (Ftp_GetReply(ftp) && ftp->nReplyCode/100==COMPLETE)
		return true;
	return false;
}
//根据大小上传缓冲区中的数据到文件
int Ftp_SendBuff(SFtp_Ftp* ftp, int BuffSize,  char * pBuff, const char* lpszNewRemoteFile)
{
	char szCommand[256];
	char sendbuf[FTP_BUFSIZE+2];
	int len = 0;
	int i = 0;
	int ret = 0;
	if (!Ftp_InitDataChunnel(ftp))
	{
		if (ftp->DataChunnel!=INVALID_SOCKET)
			FPF_SockClose(ftp->DataChunnel);
		ftp->DataChunnel = INVALID_SOCKET;
		return -1;
	}
	sprintf(szCommand, "STOR %s", lpszNewRemoteFile);
	if (!Ftp_SendCommand(ftp, szCommand) || ftp->nReplyCode/100!=PRELIM)
	{
		g_FTPErrorNo = STOR_150_;//FTP上传失败: 存储文件失败
		FPF_SockClose(ftp->DataChunnel);
		ftp->DataChunnel = INVALID_SOCKET;
		if (DEBUG) printf("2");
		return -1;
	}
	if (!Ftp_BuildDataChunnel(ftp))
	{
		FPF_SockClose(ftp->DataChunnel);
		ftp->DataChunnel = INVALID_SOCKET;
		Ftp_GetReply(ftp);
		if (DEBUG) printf("3");
		return -1;
	}

	memset(sendbuf, 0, sizeof(sendbuf));
	for(i = 0; i< BuffSize; i++)
	{
		if((ftp->nTransferType==FPD_TypeAscii) &&  (* pBuff == '\n'))
		{
			sendbuf[len++] = '\r';
		}
		sendbuf[len++] = * pBuff ++;
		if(len >= FTP_BUFSIZE)
		{
			if (FPF_SockSend(ftp->DataChunnel, sendbuf, len)!=len)
				break;
			memset(sendbuf, 0, sizeof(sendbuf));
			len = 0;
		}
	}
	if(len != 0)
	{
		FPF_SockSend(ftp->DataChunnel, sendbuf, len);
		memset(sendbuf, 0, sizeof(sendbuf));
		len = 0;
	}
	FPF_SockClose(ftp->DataChunnel);
	ftp->DataChunnel = INVALID_SOCKET;
	ret = Ftp_GetReply(ftp);
	if (ret && ftp->nReplyCode==226)
	{
		return 0;
	}
	g_FTPErrorNo = Recv_226_;//FTP上传失败: 图片未完整上传

	return -1;
}
int Ftp_PutFile(SFtp_Ftp* ftp, const char* lpszLocalFile, const char* lpszNewRemoteFile)
{
	char szCommand[256];
	char sendbuf[FTP_BUFSIZE+2];
	int ch = 0;
	int len = 0;

	FILE *fp_send = fopen(lpszLocalFile, (ftp->nTransferType==FPD_TypeAscii) ? "rt" : "rb" );
	if (fp_send==NULL)
	{
		printf("Can not read file %s\n", lpszLocalFile);
		return -1;
	}
	if (!Ftp_InitDataChunnel(ftp))
	{
		FPF_SockClose(ftp->DataChunnel);
		fclose(fp_send);
		return -1;
	}
	sprintf(szCommand, "STOR %s", lpszNewRemoteFile);
	if (!Ftp_SendCommand(ftp, szCommand) || ftp->nReplyCode/100!=PRELIM)
	{
		g_FTPErrorNo = STOR_150_;//FTP上传失败: 存储文件失败
		FPF_SockClose(ftp->DataChunnel);
		fclose(fp_send);
		return -1;
	}
	if (!Ftp_BuildDataChunnel(ftp))
	{
		FPF_SockClose(ftp->DataChunnel);
		Ftp_GetReply(ftp);
		fclose(fp_send);
		return -1;
	}

	memset(sendbuf, 0, sizeof(sendbuf));
	while(!feof(fp_send))
	{
		ch = fgetc(fp_send);
		if (ftp->nTransferType==FPD_TypeAscii && ch=='\n')
			sendbuf[len++] = '\r';
		if (ch!=EOF)
			sendbuf[len++] = (char)ch;
		if (ch==EOF || len>=FTP_BUFSIZE)
		{
			if (FPF_SockSend(ftp->DataChunnel, sendbuf, len)!=len)
				break;
			memset(sendbuf, 0, sizeof(sendbuf));
			len = 0;
		}
	}
	fclose(fp_send);
	FPF_SockClose(ftp->DataChunnel);
	
	if (Ftp_GetReply(ftp) && ftp->nReplyCode/100==COMPLETE && ch==EOF)
		return 0;
	return 0;
}

void FPF_GetFtpListInfo(SFtp_FtpList* pFtpList, char* str_list, bool list_full)
{
	char mode[10],mon[4],day[4],time[8];
	char *fullmode = "rwxrwxrwx";
		
	if (list_full)
	{
		int i = 0;
		
		pFtpList->type = str_list[0];
		if (pFtpList->type=='l')
			sscanf(str_list+1, 
				"%s %d %s %s %d %s %s %s %s %*s %s",
				mode, &pFtpList->subdir_count, 
				pFtpList->owner, pFtpList->group, &pFtpList->size, 
				mon, day, time, pFtpList->filename, pFtpList->linkname);
		else
			sscanf(str_list+1, 
				"%s %d %s %s %d %s %s %s %s",
				mode, &pFtpList->subdir_count, pFtpList->owner, 
				pFtpList->group, &pFtpList->size, 
				mon, day, time, pFtpList->filename);
		
		for(i=0;i<9;i++)
			if (mode[i]==fullmode[i])
				pFtpList->access_mode += (1 << (8-i));
		pFtpList->subdir_count -= 2;
		sprintf(pFtpList->datetime, "%s %-2s %-5s", mon, day, time);
	}else
		sscanf(str_list, "%s", pFtpList->filename);
}

int Ftp_List(SFtp_Ftp* ftp, const char* lpszPathName, bool bIsFullList, SFtp_FtpList** ppFtpList)
{
	/*char szCommand[256];
	int type = ftp->nTransferType;
	int list_count = 0;
	char list_string[512];
	int max_len = 64;
	
	Ftp_CloseList(ftp);
	if (type!=FPD_TypeAscii)
		Ftp_SetTransferType(ftp, FPD_TypeAscii);
	if (!Ftp_InitDataChunnel(ftp))
	{
		FPF_SockClose(ftp->DataChunnel);
		if (type!=FPD_TypeAscii)
			Ftp_SetTransferType(ftp, type);
		return -1;
	}
	if (bIsFullList && strncmp(ftp->strSystemType, "UNIX", 4)==0)
	{
		if (!lpszPathName || !*lpszPathName)
			strcpy(szCommand, "LIST");
		else
			sprintf(szCommand, "LIST %s", lpszPathName);
	}else
	{
		if (!lpszPathName || !*lpszPathName)
			strcpy(szCommand, "NLST");
		else
			sprintf(szCommand, "NLST %s", lpszPathName);
	}
	if (!Ftp_SendCommand(ftp, szCommand) || ftp->nReplyCode/100!=PRELIM)
	{
		FPF_SockClose(ftp->DataChunnel);
		if (type!=FPD_TypeAscii)
			Ftp_SetTransferType(ftp, type);
		return -1;
	}
	if (!Ftp_BuildDataChunnel(ftp))
	{
		FPF_SockClose(ftp->DataChunnel);
		Ftp_GetReply(ftp);
		if (type!=FPD_TypeAscii)
			Ftp_SetTransferType(ftp, type);
		return -1;
	}
	memset(list_string, 0, sizeof(list_string));
	ftp->pFtpList = (SFtp_FtpList*)malloc(max_len*sizeof(SFtp_FtpList));
	ftp->nFtpListCount = 0;
	while(true)
	{
		char recvbuf[FTP_BUFSIZE+1];
		char *p1, *p2;
		int len;
		
		memset(recvbuf, 0, FTP_BUFSIZE+1);
		len = FPF_SockReceive(ftp->DataChunnel, recvbuf, FTP_BUFSIZE);
		if (len<=0 || len>FTP_BUFSIZE)
		{
			FPF_SockClose(ftp->DataChunnel);
			break;
		}
#ifdef _DEBUG
		printf(recvbuf);
#endif
		
		p1 = recvbuf;
		p2 = list_string+strlen(list_string);
		while(*p1)
		{
			if(*p1!='\n')
				*p2++ = *p1;
			else
			if (strlen(list_string)<58)
			{
				memset(list_string, 0, sizeof(list_string));
				p2 = list_string;
			}else
			{
				if (ftp->nFtpListCount==max_len)
				{
					SFtp_FtpList *pList;
					max_len *= 2;
					pList = (SFtp_FtpList*)realloc(ftp->pFtpList, max_len*sizeof(SFtp_FtpList));
					ftp->pFtpList = pList;
				}
				FPF_GetFtpListInfo(&(ftp->pFtpList[ftp->nFtpListCount]), list_string, bIsFullList);
				ftp->nFtpListCount++;
				memset(list_string, 0, sizeof(list_string));
				p2 = list_string;
			}
			p1++;
		}
	}

	if (Ftp_GetReply(ftp) && ftp->nReplyCode/100==COMPLETE)
	{
		if (type!=FPD_TypeAscii)
			Ftp_SetTransferType(ftp, type);
		*ppFtpList = ftp->pFtpList;
		return ftp->nFtpListCount;
	}
	free(ftp->pFtpList);
	ftp->pFtpList = NULL;
	if (type!=FPD_TypeAscii)
		Ftp_SetTransferType(ftp, type);
*/
	return -1;
}

void Ftp_CloseList(SFtp_Ftp* ftp)
{
	//if (ftp->pFtpList)
	//{
	//	free(ftp->pFtpList);
	//	ftp->pFtpList = NULL;
	//}
	//ftp->nFtpListCount = 0;
}
/*************************************************/
//功能:	得到目录的最后一个节点字符串
//参数:
//		RecvBuf[in/out]:目录缓冲区
//返回: 返回字符串的长度
//说明:
//		RecvBuf要求头和尾端的字符不能时斜杠或反斜杠
/*************************************************/
static int Str_GetLastSlashNode(char* RecvBuf)
{
	int len;
	int i;
	char tmpBuf[200];

	len = strlen(RecvBuf);
	//找到最后一个斜杠或反斜杠的位置
	for(i=len-1; i>0; i--)
	{
		if(('/' == RecvBuf[i]) || ('\\' == RecvBuf[i]))
			break;
	}
	if(i==0)
		return len;
	i++;
	memset(tmpBuf, 0, 200);
	strncpy(tmpBuf, &RecvBuf[i], len);
	memset(RecvBuf, 0, len);
	strncpy(RecvBuf, tmpBuf, len-i);
	return len-i;
}
//功能:	提取当前路径
//参数:
//		RecvBuf[in/out]:字符缓冲区
//返回: -1:失败 其它:返回的长度
static int Str_GetFullPwdPath(char* RecvBuf)
{
	int len;
	int i;
	char tmpBuf[200];
	int left = 0, right = 0;

	len = strlen(RecvBuf);
	//找到第一个双引号的位置
	for(i=0; i<len; i++)
	{
		if('"' == RecvBuf[i])
		{
			left = i+1;
			break;
		}
	}
	if(i == len)
		return -1;
	//找到最后一个双引号的位置
	for(i=len-1; i>left; i--)
	{
		if('"' == RecvBuf[i])
		{
			right = i-1;
			break;
		}
	}
	if(i == left)
		return -1;
	if(right-left<=0)
		return -1;

	memset(tmpBuf, 0, 200);
	strncpy(tmpBuf, &RecvBuf[0], right-left+1);
	memset(RecvBuf, 0, len);
	strncpy(RecvBuf, tmpBuf, right-left+1);
	return right-left+1;
}
int DirectGotoDir(SFtp_Ftp* m_ftp,char* alldir)
{
	int result = 0;
	char RecvBuf[200];
	if(Ftp_SetCurrentDirectory(m_ftp, alldir)==1)
       return 1;
	//进入失败则查看当前目录是不是目标路径
	//得到当前目录
	result = Ftp_GetCurrentDirectory(m_ftp,RecvBuf);
	if(result!= 1)
		return -1;
	//已取出当前目录
	//比较当前目录和虚拟路径是否相同
	Str_GetFullPwdPath(RecvBuf);
	if(strcmp(alldir, RecvBuf) == 0)
		return 1;
	else
		return 0;
}
int checkdir(SFtp_Ftp* m_ftp,char* rootdir)
{
	char RecvBuf[200];
	int reslt = 0;
	if((reslt = Ftp_SetCurrentDirectory(m_ftp, rootdir))==1)
	       return 1;

	if (reslt == 0)
	{//上传目录不存在则建立目录
		reslt = Ftp_CreateDirectory(m_ftp, rootdir);
		//printf("zxrtest..createdir...%d\n",reslt);
		if(reslt != 1)
		{
			g_FTPErrorNo = MKD_257_;
			printf("mkdir error\n");
			return -1;
		}
	}
	if(Ftp_SetCurrentDirectory(m_ftp, rootdir)==1)  //access to current directory
		return 1;
	//得到当前目录
	reslt = Ftp_GetCurrentDirectory(m_ftp,RecvBuf);
	//printf("zxrtest..getdir...%d\n",reslt);
	if(reslt != 1)
	{
		printf("pwd error\n");
		return -1;
	}
	//得到目录的最后一个节点字符串
	Str_GetLastSlashNode(RecvBuf);
	//如果最后的节点字符串和rootdir相同，则认为建立成功
	if(strncmp(rootdir, RecvBuf, strlen(rootdir)) == 0)
		return 1;

	return -1;
}
//static SFtp_Ftp *m_ftpserv_d;
int UploadViaFTP(char * ip, char * name, char * passwd, char * filename, char * directory,char *data,int size)
{
	int ret;
	char seps[] = "/";
	char ftppass[255];
	char *subdir;
	char dirs[10][80];
	int  i,j;
	SFtp_Ftp* m_ftpserv_d;
	m_ftpserv_d = Ftp_Connect(ip, name,passwd);
	if(m_ftpserv_d == NULL)
	{
		//m_ftpserv_d = Ftp_Connect(ip, name,passwd);
		return -1;
	}

	if(ping(ip, 30, 2) == -1)//lxremark 2013-12-24 ping 3次,30毫秒
	{
		printf("lxprintf 2013-12-24 UploadViaFTP ping not found\r\n");
		return -1;
	}

	//进入指定的工作目录
	memset(ftppass,0,sizeof(ftppass));
	strcpy(ftppass,directory);
	if((ret = DirectGotoDir(m_ftpserv_d,ftppass)) == 0)
	{
		subdir = strtok(ftppass, seps);
		for(i = 0; i < 10; i++)
		{
			if(subdir != NULL)
			{
				strcpy(dirs[i], subdir);
				subdir = strtok(NULL, seps);
			}
			else
			{
				break;
			}
		}
		for(j = 0; j < i; j++)
		{
			if(checkdir(m_ftpserv_d,dirs[j]) < 0)
			{
				Ftp_Close(m_ftpserv_d);
				return -1;
			}
		}
	}
	else if(ret<0)
	{
		Ftp_Close(m_ftpserv_d);
		return -1;
	}
	//上传文件
	ret = Ftp_SendBuff(m_ftpserv_d, size, data,filename);
	if(ret == -1)
		printf("ftp upload picture failed\n");
	Ftp_Close(m_ftpserv_d);
	return ret;
}
